<?php

namespace App\Exports;


use App\Models\Cert\CertDriver;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
class DriverExport extends DefaultValueBinder implements FromArray,WithCustomValueBinder
{

    private $conditions;

    public function __construct($conditions) {
        $this->conditions = $conditions;
    }

    public function array():array {

        $data = [
            // 设置表头信息
            ['序号','姓名','性别','身份证','手机','住址','类型','领证日期','有效日期','创建时间'],
        ];

        $records = (new CertDriver())->getLst($this->conditions)->with(['getUser'])->get();

        foreach ($records as $record) {
            $temp = [
                $record->id,
                $record->name??'',
                $record->gender??'',
                $record->id_card,
                $record->getUser->phone??'',
                $record->address??'',
                $record->allow_car_type??'',
                $record->get_cert_day??'',
                $record->effective_day??'',
                $record->created_at??'',
            ];
            $data[] = $temp;
        }
        return $data;


    }
    public function bindValue(Cell $cell, $value) {
        if (strlen($value) > 10) {
            $cell->setValueExplicit($value, DataType::TYPE_STRING);
            return true;
        }
        return parent::bindValue($cell, $value);
    }
}
